package gugga.collections.linkedList
{
	import gugga.collections.IIterator;
	import gugga.debug.Assertion;
	
	public class LinkedListIterator implements IIterator
	{
		private var mCurrentItem : LinkedListItem;
		private var mStartItem : LinkedListItem;
		
		private var mIsValid : Boolean = false;
		public function get IsValid() : Boolean { return mIsValid; }
		
		public function get Current() : Object 
		{
			return current();
		}
		
		public function get CurrentListItem() : LinkedListItem 
		{
		 	Assertion.failIfFalse(
				mIsValid, 
				"There is no valid current item", this, arguments);
					
			return mCurrentItem;
		}
		
		public function LinkedListIterator(aStartItem : LinkedListItem)
		{
			mStartItem = aStartItem;
			reset();
		}
		
		public function reset() : void
		{
			mCurrentItem = new LinkedListItem();
			mCurrentItem.NextItem = mStartItem;
			
			mIsValid = false;
		}
	
		public function iterate() : Boolean 
		{
			mCurrentItem = mCurrentItem.NextItem;
			
			if(mCurrentItem)
			{	
				mIsValid = true;
				return true;
			}
			else
			{
				mIsValid = false;
				return false;
			}
		}
	
		public function current() : Object 
		{
			Assertion.failIfFalse(
				mIsValid, 
				"There is no valid value of the current item", this, arguments);
					
			return mCurrentItem.Data;
		}
	}
}